import {
  Controller,
  Get,
  Post,
  Req,
} from '@nestjs/common'
import { ApiTags } from '@nestjs/swagger'
import { JwtNeed } from '@/common/decorator'
import { WheelRecordService } from '../wheel-record/wheel-record.service'
import { WheelPrizeService } from './wheel-prize.service'

@ApiTags('官网/二期/奖品')
@Controller('web/wheel_prize')
export class WebWheelPrizeController {
  constructor(
    private readonly wheelPrizeService: WheelPrizeService,
    private readonly wheelRecordService: WheelRecordService,
  ) {}

  /**
   * 奖品列表
   */
  @Get()
  webList() {
    return this.wheelPrizeService.findAll({ where: { isEnabled: true, isDeleted: false } })
  }

  /**
   * 抽奖
   */
  @Post('draw')
  @JwtNeed()
  async draw(@Req() req: Request) {
    const prize = await this.wheelPrizeService.draw()

    await this.wheelRecordService.save({
      wheelPrizeId: prize.wheelPrizeId,
      userUid: req.user.uid,
    })
    return prize
  }
}
